package com.asiainfo.ssm.execute.dao.impl;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import com.asiainfo.ssm.execute.bo.BOExecInstrStatChaEngine;
import com.asiainfo.ssm.execute.dao.interfaces.IExecInstrStatChaDAO;
import com.asiainfo.ssm.execute.ivalues.IBOExecInstrStatChaValue;

import org.apache.commons.lang.StringUtils;


public class ExecInstrStatChaDAOImpl implements IExecInstrStatChaDAO {

	public long saveExecInstrStatCha(IBOExecInstrStatChaValue execInstrStatChaValue) throws Exception, RemoteException {
		long id = -1;
		if (execInstrStatChaValue.isNew()) {
			long newId = BOExecInstrStatChaEngine.getNewId().longValue();
			execInstrStatChaValue.setId(newId);
			id = newId;
		} else {
			id = execInstrStatChaValue.getId();
		}
		BOExecInstrStatChaEngine.save(execInstrStatChaValue);
		return id;
	}

	public void saveExecInstrStatCha(IBOExecInstrStatChaValue[] execInstrStatChaValue) throws Exception, RemoteException {
		for (IBOExecInstrStatChaValue boExecInstrStatChaValue : execInstrStatChaValue) {
			long id = -1;
			if (boExecInstrStatChaValue.isNew()) {
				long newId = BOExecInstrStatChaEngine.getNewId().longValue();
				boExecInstrStatChaValue.setId(newId);
				boExecInstrStatChaValue.setDoneDate(BOExecInstrStatChaEngine.getSysDate());
				id = newId;
			} else {
				id = boExecInstrStatChaValue.getId();
			}
		}
		BOExecInstrStatChaEngine.saveBatch(execInstrStatChaValue);
	}

	public IBOExecInstrStatChaValue[] qryExecInstrStatChaByCond(long id, String actId,
			String criticalPointId, String act_status, String doneDate, String remark,
			int startNum, int endNum)throws Exception, RemoteException {

		StringBuffer sqlCond = new StringBuffer();
		Map paraMap = new HashMap();
		sqlCond.append(" 1=1 ");
		if (id > 0) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_Id).append(" =:id ");
			paraMap.put("id", id);
		}

		if (!StringUtils.isEmpty(actId)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_ActId).append(" =:actId ");
			paraMap.put("actId", actId);
		}

		if (!StringUtils.isEmpty(criticalPointId)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_CriticalPointId).append(" =:criticalPointId ");
			paraMap.put("criticalPointId", criticalPointId);
		}

		if (!StringUtils.isEmpty(act_status)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_ActStatus).append(" =:act_status ");
			paraMap.put("act_status", act_status);
		}		

		if (StringUtils.isNotBlank(doneDate)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_DoneDate).append(" =:doneDate ");
			paraMap.put("doneDate", doneDate);
		}
		
		if (!StringUtils.isEmpty(remark)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_Remark).append(" =:remark ");
			paraMap.put("remark", remark);
		}

		IBOExecInstrStatChaValue[] values = BOExecInstrStatChaEngine.getBeans(null,	sqlCond.toString(), paraMap, startNum, endNum, false);
		return values;
	}

	public int qryExecInstrStatChaCountByCond(long id, String actId, String criticalPointId, String act_status, String doneDate, String remark) throws Exception, RemoteException {
		StringBuffer sqlCond = new StringBuffer();
		Map paraMap = new HashMap();
		sqlCond.append(" 1=1 ");
		if (id > 0) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_Id).append(" =:id ");
			paraMap.put("id", id);
		}

		if (!StringUtils.isEmpty(actId)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_ActId).append(" =:actId ");
			paraMap.put("actId", actId);
		}

		if (!StringUtils.isEmpty(criticalPointId)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_CriticalPointId).append(" =:criticalPointId ");
			paraMap.put("criticalPointId", criticalPointId);
		}

		if (!StringUtils.isEmpty(act_status)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_ActStatus).append(" =:act_status ");
			paraMap.put("act_status", act_status);
		}		

		if (StringUtils.isNotBlank(doneDate)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_DoneDate).append(" =:doneDate ");
			paraMap.put("doneDate", doneDate);
		}
		
		if (!StringUtils.isEmpty(remark)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_Remark).append(" =:remark ");
			paraMap.put("remark", remark);
		}

		int count = BOExecInstrStatChaEngine.getBeansCount(sqlCond.toString(), paraMap);
		return count;
	}

/*	public IBOGetExecInstrStatChaValue getExecInstrStatChaByCond(String actId, String criticalPointId) throws Exception, RemoteException {
		StringBuffer sqlCond = new StringBuffer();
		Map paraMap = new HashMap();
		
		if (!StringUtils.isEmpty(actId)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_ActId).append(" =:actId ");
			paraMap.put("actId", actId);
		}
		
		if (!StringUtils.isEmpty(criticalPointId)) {
			sqlCond.append(" AND ").append(IBOExecInstrStatChaValue.S_CriticalPointId).append(" =:criticalPointId ");
			paraMap.put("criticalPointId", criticalPointId);
		}
		
		IBOGetExecInstrStatChaValue[] values = BOGetExecInstrStatChaEngine.getBeans(sqlCond.toString(), paraMap);
		IBOGetExecInstrStatChaValue value = values[0];
		return value;
	}*/
	
}
